package org.fhm.zdte.client.service;

import org.fhm.substrate.annotation.Component;
import org.fhm.substrate.annotation.Setup;
import org.fhm.zdte.client.ability.IGetConnectChannelType;
import org.fhm.zdte.common.constant.ChannelType;
import org.fhm.zdte.common.pojo.Address;
import org.fhm.zdte.common.service.channel.AbstractChannelDispatcher;
import org.fhm.zdte.common.standard.IChannelManager;

import java.util.Objects;

/**
 * The common channel dispatcher.
 *
 * @author 谭波
 * @since 2024/4/7
 */
@Component
public class ClientChannelDispatcher extends AbstractChannelDispatcher {

    @Setup
    private IGetConnectChannelType iGetConnectChannelType;

    @Override
    public IChannelManager obtainChannelManager(Address address) {
        ChannelType channelType = iGetConnectChannelType.obtainChannelType(address);
        IChannelManager defaultChannel = iChannelManagerMap.get(ChannelType.DEFAULT.name());
        IChannelManager iChannelManager;
        if (Objects.isNull(channelType) || Objects.isNull((iChannelManager = iChannelManagerMap.get(channelType.name()))))
            return defaultChannel;
        else {
            System.out.println("使用备用通道");
            iChannelManager.initializeChannelManager();
            return iChannelManager;
        }
    }

}
